package com.wyu.blog.controller;

import com.wyu.blog.dto.RespDTO;
import com.wyu.blog.entity.*;
import com.wyu.blog.service.*;
import com.wyu.blog.util.BlogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@RestController
//@RequestMapping(value = "/admin")
public class BackController {

    @Autowired
    private UserService userService;

    @Autowired
    private SortService sortService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserFriendService userFriendService;



    /**
     * 查询所有用户信息
     */
    @GetMapping(value = "/user/list")
    public List<User> findUserList(){
        return userService.findAllUser();
    }

    /**
     * 根据ID查询用户信息
     */
    @GetMapping(value = "/admin/user/{id}")
    public User findUserById(@PathVariable Integer id){
        return userService.findUserByUserId(id);
    }


    /**
     * 根据id删除用户
     */
    @DeleteMapping(value = "/admin/user/{id}")
    public RespDTO deleteUserById(@PathVariable Integer id){
        userService.deleteUser(id);
        return RespDTO.ok("删除成功");
    }

    /**
     * 添加用户信息
     */
    @PostMapping(value = "/admin/user")
    public RespDTO addUser(@RequestBody User user){
        User user1 = userService.findUserByName(user.getUserName());
        if(user1 == null) {
            if (user.getUserName() != null && user.getUserPassword() != null && user.getUserEmail() != null
                    && user.getUserTelephoneNumber() != null && user.getUserIntroduction() != null &&
                    user.getUserAge() != null) {
                user.setUserSalt(UUID.randomUUID().toString().substring(0, 5));
                user.setUserPassword(BlogUtil.MD5(user.getUserPassword() + user.getUserSalt()));
                user.setUserRegistrationTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                userService.addUserByBack(user);
                return RespDTO.ok("添加成功");
            } else{
                return RespDTO.error("请输入完整个人信息");
            }
        }
        return RespDTO.error("添加失败，该用户已存在");
    }

    /**
     * 根据ID修改用户信息
     */
    @PutMapping(value = "/admin/user/{id}")
    public RespDTO updateUserById(@PathVariable Integer id, @RequestBody User user) {
//        user.setUserSalt(UUID.randomUUID().toString().substring(0, 5));
//        user.setUserPassword(BlogUtil.MD5(user.getUserPassword() + user.getUserSalt()));
        userService.updateUser(user);
        return RespDTO.ok("修改成功");
    }

    //******************************************

    /**
     * 查询所有分类信息
     */
    @GetMapping(value = "/category/list")
    public List<Category> findCategoryList(){
        return articleService.findAllCategory();
    }

    /**
     * 添加分类信息
     */
    @PostMapping(value = "/admin/category")
    public RespDTO addCategory(@RequestBody Sort sort){
        Sort sort1 = sortService.findSortByName(sort.getSortName());
        if(sort1 == null) {
            if(sort.getSortName() == null || sort.getSortName().equals("")){
                return RespDTO.error("请输入分类名");
            }else {
                sortService.addSort(sort);
                return RespDTO.ok("添加成功");
            }
        }
        return RespDTO.error("添加失败，该分类已经存在");
    }

    /**
     * 根据ID查询分类信息
     */
    @GetMapping(value = "/admin/category/{id}")
    public Category findCategoryById(@PathVariable Integer id){
        return articleService.findCategoryBySortId(id);
    }

    /**
     * 根据id删除分类信息
     */
    @DeleteMapping(value = "/admin/category/{id}")
    public RespDTO deleteCategoryById(@PathVariable Integer id) {
        sortService.deleteSort(id);
        return RespDTO.ok("删除成功");
    }

    /**
     * 根据ID修改分类信息
     * @return
     */
    @PutMapping(value = "/admin/category/{id}")
    public RespDTO updateCategoryById(@PathVariable Integer id,@RequestBody Sort sort){
        sortService.updateSort(sort);
        return RespDTO.ok("修改成功");
    }



    //******************************************


    /**
     * 查询所有文章信息
     */
    @GetMapping(value = "/article/list")
    public List<Article> findArticleList(){
        return articleService.findAllArticle();
    }

    /**
     * 根据分类ID查询文章信息
     */
    @GetMapping(value = "/article/list/sort/{id}")
    public List<Article> findArticleById(@PathVariable Integer id){
        return articleService.findArticleBySortId(id);
    }

    /**
     * 根据文章ID查询文章信息
     */
    @GetMapping(value = "/admin/article/{id}")
    public Article findArticleByArticleId(@PathVariable Integer id){
        return articleService.findArticleById(id);
    }


    /**
     * 根据id删除文章信息
     */
    @DeleteMapping(value = "/admin/article/{id}")
    public RespDTO deleteArticleById(@PathVariable Integer id) {
        articleService.deleteArticle(id);
        return RespDTO.ok("删除成功");
    }


    /**
     * 根据ID修改文章信息
     * @return
     */
    @PutMapping(value = "/admin/article/{id}")
    public RespDTO updateArticleById(@PathVariable Integer id,@RequestBody Article article){
        articleService.updateArticleById(article);
        return RespDTO.ok("修改成功");
    }

    //******************************************

    /**
     * 查询所有评论信息
     */
    @GetMapping(value = "/comment/list")
    public List<Comment> findCommentList(){
        return commentService.findAllComment();
    }

    /**
     * 根据文章ID查询评论信息
     */
    @GetMapping(value = "/comment/list/sort/{id}")
    public List<Comment> findCommentById(@PathVariable Integer id){
        return commentService.findCommentByArticleId1(id);
    }


    /**
     * 根据id删除评论信息
     */
    @DeleteMapping(value = "/admin/comment/{id}")
    public RespDTO deleteCommentById(@PathVariable Integer id) {
        commentService.deleteComment(id);
        Comment comment = commentService.findComment(id);
        Article article = articleService.findArticleById(comment.getArticleId());
        article.setArticleCommentCount(article.getArticleCommentCount() + 1);
        articleService.updateComment(article);
        return RespDTO.ok("删除成功");
    }


    //*******************************************


    /**
     * 查询所有分类名称
     */
    @GetMapping(value = "/sort/list")
    public List<Sort> findAllSort(){
        return sortService.findAllSort();
    }


}
